home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15378 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  62 lines

  1. Path: user2.mnsinc.com!huang
  2. From: huang@mnsinc.com (Szu-Wen Huang)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: [Q] Best handling of non-fixed-length data??
  5. Date: 18 Apr 1996 19:45:59 GMT
  6. Organization: Monumental Network Systems
  7. Message-ID: <4l665n$9uq@news1.mnsinc.com>
  8. References: <4l629f$4cc@nuke.csu.net>
  9. NNTP-Posting-Host: user2.mnsinc.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Eric Brower (etb1@Axe.Humboldt.edu) wrote:
  13.  
  14. : Here is my application:  I would like to store exam questions, along with
  15. : the correct answer and as many other possible answers per question, in a 
  16. : file.  From this file, I would like to be able to extract the question
  17. : and all possible answers through the question number I assign each question
  18. : within the database.  My problem lies in memory management, as well as
  19. : method of storing this data.  You see, I'd like questions to be as long
  20. : as need be, without using hideous amounts of space for the short questions
  21. : as well.  As well, some questions may have only two possible answers, while
  22. : other may have five or more.  I also must be able to extract the correct 
  23. : answer from among the possibles.
  24.  
  25. Here is my suggestion.  Let me try to first jot down the assumptions:
  26.  
  27. 1.  any number of items
  28. 2.  1 question per item
  29. 3.  2 or more answers per question
  30. 4.  1 correct answer per question
  31.  
  32. Okay, starting from the top going down, requirement #1 clearly indicates
  33. the need for a dynamic data structure.  Since your question database is
  34. next to useless if you only have 10 or 20 questions, a linked-list might
  35. not be such a good idea.  I'd suggest a binary tree.
  36.  
  37. Requirement #2 is easy.  A question is likely a string of characters.
  38. Since you don't want to waste space and the variance of the question
  39. lengths is large, the immediately obvious solution is to keep a char *
  40. instead of a char array.
  41.  
  42. Requirement #3 is a bit tough.  A linked-list of answers springs to
  43. mind, but if you know the maximum number of possible answers and it's
  44. fairly small, an array of pointers might work better.  Based on the same
  45. variable-length string constraint as #2, this is an array of char *.
  46.  
  47. Requirement #4 is certainly trivial.  One answer is correct, so it's
  48. stored in a char.  It's not likely that you'll have so many possible
  49. answers to need something more than a char - if you do, use an int.
  50.  
  51. Okay, so our structure might look as follows:
  52.  
  53. struct item {
  54.   struct item *left_son,         /* pointer to sons of the node, used */
  55.               *right_son;        /* to implement the binary tree */
  56.   char *question;
  57.   char *answers[MAX_POSSIBLE_ANSWERS];
  58.   char correct_answer;
  59. };
  60.  
  61. Ta-dah ;)
  62.